package com.example.oms.common.mybatis;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.example.oms.common.utils.MyBatisUtils;
import com.example.oms.common.config.PageParam;
import com.example.oms.common.config.PageResult;
import com.github.yulichang.base.MPJBaseMapper;
import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface BaseMapperX<T> extends MPJBaseMapper<T> {
    default PageResult<T> selectPage(PageParam pageParam, @Param("ew") Wrapper<T> queryWrapper) {
        IPage<T> mpPage = MyBatisUtils.buildPage(pageParam);
        this.selectPage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal());
    }

    default T selectOne(String field, Object value) {
        return (T) this.selectOne((Wrapper)(new QueryWrapper()).eq(field, value));
    }

    default T selectOne(SFunction<T, ?> field, Object value) {
        return (T) this.selectOne((Wrapper)(new LambdaQueryWrapper()).eq(field, value));
    }

    default T selectOne(String field1, Object value1, String field2, Object value2) {
        return (T) this.selectOne((Wrapper)((QueryWrapper)(new QueryWrapper()).eq(field1, value1)).eq(field2, value2));
    }

    default T selectOne(SFunction<T, ?> field1, Object value1, SFunction<T, ?> field2, Object value2) {
        return (T) this.selectOne((Wrapper)((LambdaQueryWrapper)(new LambdaQueryWrapper()).eq(field1, value1)).eq(field2, value2));
    }

    default Long selectCount() {
        return this.selectCount(new QueryWrapper());
    }

    default Long selectCount(String field, Object value) {
        return this.selectCount((Wrapper)(new QueryWrapper()).eq(field, value));
    }

    default Long selectCount(SFunction<T, ?> field, Object value) {
        return this.selectCount((Wrapper)(new LambdaQueryWrapper()).eq(field, value));
    }

    default List<T> selectList() {
        return this.selectList(new QueryWrapper());
    }

    default List<T> selectList(String field, Object value) {
        return this.selectList((Wrapper)(new QueryWrapper()).eq(field, value));
    }

    default List<T> selectList(SFunction<T, ?> field, Object value) {
        return this.selectList((Wrapper)(new LambdaQueryWrapper()).eq(field, value));
    }

    default List<T> selectList(String field, Collection<?> values) {
        return this.selectList((Wrapper)(new QueryWrapper()).in(field, values));
    }

    default List<T> selectList(SFunction<T, ?> field, Collection<?> values) {
        return this.selectList((Wrapper)(new LambdaQueryWrapper()).in(field, values));
    }

    default List<T> selectList(SFunction<T, ?> leField, SFunction<T, ?> geField, Object value) {
        return this.selectList((Wrapper)((LambdaQueryWrapper)(new LambdaQueryWrapper()).le(leField, value)).ge(geField, value));
    }

    default void insertBatch(Collection<T> entities) {
        Db.saveBatch(entities);
    }

    default void insertBatch(Collection<T> entities, int size) {
        Db.saveBatch(entities, size);
    }

    default void updateBatch(T update) {
        this.update(update, new QueryWrapper());
    }

    default void updateBatch(Collection<T> entities, int size) {
        Db.updateBatchById(entities, size);
    }
}